<template>
    <div>
        <el-container>
            <el-header>
                <img class="logo_img" src="https://cn.vuejs.org/images/logo.svg" alt="">
            </el-header>
            <el-main>
                <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="100px" class="demo-loginForm">
                    <el-form-item label="登陆账号" prop="user">
                        <el-input v-model="loginForm.user"></el-input>
                    </el-form-item>
                    <el-form-item label="登陆密码" prop="pwd">
                        <el-input v-model="loginForm.pwd" type="password"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm('loginForm')">登陆</el-button>
                        <el-button @click="resetForm('loginForm')">忘记密码</el-button>
                    </el-form-item>
                </el-form>
            </el-main>
        </el-container>
    </div>
</template>
<script>
export default {
    name: "Login",
    data() {
      return {
        loginForm: {
          user: 'hello',
          pwd: 'helloworld'
        },
        rules: {
          user: [
            { required: true, message: '请输入账号', trigger: 'blur' },
            { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
          ],
          pwd: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        const _this = this;
        this.$refs[formName].validate((valid)=>{
          if(valid){
            this.$axios.post("/login", this.loginForm).then(res =>{
              const token = res.headers['authorization'];
              const userInfo = res.data.data;
              _this.$store.commit('setToken', token);
              _this.$store.commit('setUserInfo', userInfo);
              _this.$router.push('/blogs')
            });
          }else{
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
}
</script>
<style scoped>
  .el-header, .el-footer {
    background-color: #B3C0D1;
    color: #333;
    text-align: center;
    line-height: 60px;
  }
  
  .el-aside {
    background-color: #D3DCE6;
    color: #333;
    text-align: center;
    line-height: 200px;
  }
  
  .el-main {
    /* background-color: #E9EEF3; */
    color: #333;
    text-align: center;
    line-height: 160px;
  }
  
  body > .el-container {
    margin-bottom: 20px;
  }
  
  .el-container:nth-child(5) .el-aside,
  .el-container:nth-child(6) .el-aside {
    line-height: 260px;
  }
  
  .el-container:nth-child(7) .el-aside {
    line-height: 320px;
  }

  .logo_img {
      height: 60%;
      margin: 10px 0px;
  }

  .demo-loginForm {
      width: 500px;
      margin: 0 auto;
  }
</style>